import {createRouter,createWebHashHistory} from 'vue-router'
import Ceshiren from './components/Ceshiren.vue'
import Hogwarts from './components/Hogwarts.vue'
import HogChild from './components/HogChild.vue'
import Redirect from './components/Redirect.vue'

import Params from "./components/Params.vue"
import Search from "./components/Search.vue"
import CodeHome from "./components/CodeHome.vue"
import CodeChild from "./components/CodeChild.vue"

 // 导入 视图组件
 import Home from "./views/Home.vue"
 import About from "./views/About.vue"
 import News from "./views/News.vue"

const routes=[
    {
        path:"/ceshiren",
        component:Ceshiren,
        alias: ["/haha", '/ha']
    },
    {
        path:'/hogwarts',
        component:Hogwarts,
        children:[
            {
                path:"hogchild",
                component:HogChild,
                name: 'hogchildname'
            }
        ]
    },
    {
        path:"/redirect",
        component:Redirect,
        redirect:{path: "/ceshiren"}
    },
    {
        path:'/codehome',
        component:CodeHome,
        // 设置多个别名
        children:[
            {
                path: "codechild",
                component: CodeChild,
                name: 'codechild',
            }
        ]
    },
    {
        path:'/search',
        name:'search',
        component:Search
    },
    {
        path:"/params/:userId",
        name:"userWithParam",
        component:Params
    },
    {   
        path: "/",  
        component: Home 
    },
    {   
        path: "/home",  
        component: Home, 
        name: "Home" 
    },
    {   
        path: "/about", 
        component: About, 
        name: "About" 
    },
    {   
        path: "/news",  
        component: News, 
        name: "News" 
    }
]

const router = createRouter({
    // 路由模式
    history:createWebHashHistory(),
    routes,// `routes:routes` 的缩写
});

 // 添加路由守卫
 // 添加全局前置守卫
 router.beforeEach((to, from)=>{
    console.log("beforeEach -- from:", from, "to:", to)
})
// 添加全局解析守卫
router.beforeResolve((to, from)=>{
    console.log("beforeResolve -- from:", from, "to:", to)
})
// 添加全局后置钩子
router.afterEach((to, from)=>{
    console.log("afterEach -- from:", from, "to:", to)
    return "/home"
})

export default router
